package main.java.com.uycode.serializable;

import java.io.*;
import java.text.MessageFormat;

public class TestSerializableAndDeserializable {

    public static void main(String[] args) throws Exception{
        SerializeStudent();

        Student student = DeserializeStudent();

        System.out.println(MessageFormat.format("name={0}, age={1}, grade={2}",
                student.getName(), student.getAge(), student.getGrade()));

    }

    private static void SerializeStudent () throws FileNotFoundException,
            IOException {
        Student student = new Student();

        student.setName("ahmatjan");
        student.setAge("24");
        student.setGrade("90");

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream(new File("D:/test1.txt"))
        );
        objectOutputStream.writeObject(student);
        System.out.println("Serialize Class Student Success!");
        objectOutputStream.close();

    }
    private static Student DeserializeStudent () throws Exception, IOException {
        ObjectInputStream objectInputStream = new ObjectInputStream(
                new FileInputStream(new File("D:/test.txt"))
        );
        Student student = (Student) objectInputStream.readObject();
        System.out.println("Deserialize Class Student Success!");
        return student;
    }
}
